export const network_init = function (_, generator) {
    generator.definitions_['import network_no'] = "import network_no";
    var varName = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    var mode = this.getFieldValue('mode');
    return "" + varName + " = network_no.wifi_init" + mode + "()\n";
}

export const network_scan = function (_, generator) {
    generator.definitions_['import network_no'] = "import network_no";
    var varName = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    var code = "network_no.scans(" + varName + ")";
    return [code, generator.ORDER_ATOMIC];
}

export const network_connect = function (_, generator) {
    generator.definitions_['import network_no'] = "import network_no";
    var varName = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    var account = generator.valueToCode(this, 'account', generator.ORDER_ATOMIC);
    var passwor = generator.valueToCode(this, 'password', generator.ORDER_ATOMIC);
    var code = "" + varName + ".connect(" + account + "," + passwor + ")\n";
    return code;
}

export const network_ifconfig = function (_, generator) {
    generator.definitions_['import network_no'] = "import network_no";
    var varName = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    var mode = this.getFieldValue('mode');
    var code = '';
    if (mode == "1")
        code = "" + varName + ".ifconfig()";
    if (mode == "2")
        code = "" + varName + ".isconnected()";
    return [code, generator.ORDER_ATOMIC];
}

export const network_disconnect = function (_, generator) {
    generator.definitions_['import network_no'] = "import network_no";
    var varName = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    var code = "" + varName + ".disconnect()\n";
    return code;
}

export const network_enable_ap = function (_, generator) {
    generator.definitions_['import network_no'] = "import network_no";
    var varName = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    var account = generator.valueToCode(this, 'account', generator.ORDER_ATOMIC);
    var passwor = generator.valueToCode(this, 'password', generator.ORDER_ATOMIC);
    var chl = generator.valueToCode(this, 'chl', generator.ORDER_ATOMIC);
    var mode = this.getFieldValue('mode');
    var code = "" + varName + ".enable_ap(" + account + "," + passwor + "," + chl + "," + varName + "." + mode + ")\n";
    return code;
}

export const network_disable_ap = function (_, generator) {
    generator.definitions_['import network_no'] = "import network_no";
    var varName = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    var code = "" + varName + ".disable_ap()\n";
    return code;
}

export const network_server = function (_, generator) {
    generator.definitions_['import_server_*'] = 'from server import *';
    var varName = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    // generator.setups_['class_wlan'] ='SSID="ying"\n'+'PASSWORD="201411132040"\n';
    return 'if not ' + varName + '.isconnected():\n'
        + '    connectWifi(SSID, PASSWORD)\n'
        + 'ip=' + varName + '.ifconfig()[0]\n'
        + 'print(ip)\n'
        + 'time.sleep(1)\n'
        + 'DATA=listenData()\n'
}

//ok
export const network_socket_init = function (_, generator) {
    generator.definitions_['import_socket'] = "import socket";
    var varName = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    var mode = this.getFieldValue('mode');
    if (mode == 'UDP') {
        mode = 'socket.SOCK_DGRAM'
    }
    else if (mode == 'TCP') {
        mode = 'socket.SOCK_STREAM'
    }
    return "" + varName + " = socket.socket(" + mode + ")\n";
}

//ok
export const network_socket_getaddrinfo = function (_, generator) {
    generator.definitions_['import_socket'] = "import socket";
    var addr = generator.valueToCode(this, 'addr', generator.ORDER_ATOMIC);
    var code = "socket.getaddrinfo(" + addr + ",80)[0][-1]";
    return [code, generator.ORDER_ATOMIC];
}

//ok
export const network_socket_bind = function (_, generator) {
    generator.definitions_['import_socket'] = "import socket";
    var varName = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    var address = generator.valueToCode(this, 'address', generator.ORDER_ATOMIC);
    return "" + varName + ".bind(" + address + ")\n";
}

export const network_socket_connect = function (_, generator) {
    generator.definitions_['import_socket'] = "import socket";
    var varName = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    var address = generator.valueToCode(this, 'address', generator.ORDER_ATOMIC);
    return "" + varName + ".connect(" + address + ")\n";
}

export const network_socket_listen = function (_, generator) {
    generator.definitions_['import_socket'] = "import socket";
    var varName = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    var queue = generator.valueToCode(this, 'queue', generator.ORDER_ATOMIC);
    return "" + varName + ".listen(" + queue + ")\n";
}

export const network_socket_accept = function (_, generator) {
    generator.definitions_['import_socket'] = "import socket";
    var varName = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    var code = "" + varName + ".accept()";
    return [code, generator.ORDER_ASSIGNMENT];
}

export const network_socket_receive = function (_, generator) {
    generator.definitions_['import_socket'] = "import socket";
    var varName = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    var size = generator.valueToCode(this, 'size', generator.ORDER_ATOMIC);
    var code = "" + varName + ".recv(" + size + ")";
    return [code, generator.ORDER_ASSIGNMENT];
}

export const network_socket_send = function (_, generator) {
    generator.definitions_['import_socket'] = "import socket";
    var varName = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    var content = generator.valueToCode(this, 'content', generator.ORDER_ATOMIC);
    var code = "" + varName + ".send(" + content + ")\n";
    return code;
}

export const network_socket_receive_from = function (_, generator) {
    generator.definitions_['import_socket'] = "import socket";
    var varName = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    var size = generator.valueToCode(this, 'size', generator.ORDER_ATOMIC);
    var code = "" + varName + ".recvfrom(" + size + ")";
    return [code, generator.ORDER_ASSIGNMENT];
}

export const network_socket_send_to = function (_, generator) {
    generator.definitions_['import_socket'] = "import socket";
    var varName = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    var content = generator.valueToCode(this, 'content', generator.ORDER_ATOMIC);
    var address = generator.valueToCode(this, 'address', generator.ORDER_ATOMIC);
    var code = "" + varName + ".sendto(" + content + "," + address + ")\n";
    return code;
}

export const network_socket_close = function (_, generator) {
    generator.definitions_['import_socket'] = "import socket";
    var varName = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    var code = "" + varName + ".close()\n";
    return code;
}

//---------------TABIT WiFi ESP32资源---20230520--------------------
export const network_tabit_scan = function (_, generator) {
    generator.definitions_['from tabit import *'] = 'from tabit import *';
    var code = "wifi.scan()";
    return [code, generator.ORDER_ASSIGNMENT];
}

export const network_tabit_connect = function (_, generator) {
    generator.definitions_['from tabit import *'] = 'from tabit import *';
    var account = generator.valueToCode(this, 'account', generator.ORDER_ATOMIC);
    var passwor = generator.valueToCode(this, 'password', generator.ORDER_ATOMIC);
    var code = "wifi.connect(" + account + "," + passwor + ")\n";
    return code;
}

export const network_tabit_ifconfig = function (_, generator) {
    generator.definitions_['from tabit import *'] = 'from tabit import *';
    var mode = this.getFieldValue('mode');
    var code = '';
    if (mode == "1")
        code = "wifi.ifconfig()";
    if (mode == "2")
        code = "wifi.isconnected()";
    return [code, generator.ORDER_ASSIGNMENT];
}

export const network_tabit_disconnect = function (_, generator) {
    generator.definitions_['from tabit import *'] = 'from tabit import *';
    var code = "wifi.disconnect()\n";
    return code;
}

export const network_tabit_ping = function (_, generator) {
    generator.definitions_['from tabit import *'] = 'from tabit import *';
    var ping = generator.valueToCode(this, 'ping', generator.ORDER_ATOMIC);
    var code = "wifi.ping(" + ping + ")";
    return [code, generator.ORDER_ASSIGNMENT];
}